/**
 * small-spring
 *
 * @author yuanchen
 * @date 2021-06-29 15:22
 */
package cn.bugstack.springframework.beans.factory.support;


import cn.bugstack.springframework.beans.factory.BeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;

import java.util.Objects;

/**
 * @author yuanchen
 * @date 2021-06-29 15:22
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory  {

    @Override
    public Object getBean(String beanName) {
        return doGetBean(beanName,null);
    }

    @Override
    public Object getBean(String beanName, Object... args) {
        return doGetBean(beanName,args);
    }

    protected Object doGetBean(final String beanName, final Object[] args){
        BeanDefinition beanDefinition = getBeanDefinition(beanName);

        Object bean=null;

        if(beanDefinition.isSingleton()){
            bean = getSingleton(beanName);
            if(Objects.nonNull(bean)){
                return bean;
            }
        }

        return createBean(beanName, beanDefinition,args);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName);


    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition,Object[] args);
}
